package com.ruoyi.web.controller.weixin;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.WechatConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.wechat.domain.BskBanner;
import com.ruoyi.wechat.service.IBskBannerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/bsk/banner")
@Slf4j
public class BannerController extends BaseController {

    private String prefix = "basketball/banner";

    @Autowired
    private IBskBannerService bannerService;

    @GetMapping
    public String banner(){
        return prefix + "/banner";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectBannerList(BskBanner banner){
        startPage();
        List<BskBanner> list = bannerService.selectBannerList(banner);
        return getDataTable(list);
    }

    /**
     * 新增首页banner
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }


    /**
     * 新增保存首页banner
     */
    @RequiresPermissions("basketball:banner:add")
    @Log(title = "横幅管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BskBanner banner)
    {

        if (WechatConstants.BANNER_NAME_NOT_UNIQUE.equals(bannerService.checkBannerNameUnique(banner)))
        {
            return error("保存banner'" + banner.getBannerName() + "'失败，改banner名称已存在");
        }
        banner.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(bannerService.insertBanner(banner));
    }

    /**
     * 修改首页banner
     */
    @GetMapping("/edit/{bannerId}")
    public String edit(@PathVariable("bannerId") Long bannerId, ModelMap mmap)
    {
        mmap.put("banner", bannerService.selectBannerById(bannerId));
        return prefix + "/edit";
    }

    /**
     * 修改保存首页banner
     */
    @RequiresPermissions("basketball:banner:edit")
    @Log(title = "首页banner", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BskBanner banner)
    {
        banner.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(bannerService.updateBanner(banner));
    }

    /**
     * 删除公告
     */
    @RequiresPermissions("basketball:banner:remove")
    @Log(title = "首页banner", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bannerService.deleteBannerByIds(ids));
    }

    /**
     * 保存头像
     */
    @PostMapping("/uploadBanner")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("file") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
               String imgPath = FileUploadUtils.upload(RuoYiConfig.getBannerPath(), file);

                return AjaxResult.success("上传成功",imgPath);
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("上传banner失败！", e);
            return error(e.getMessage());
        }
    }



}
